package tmp;
import logic.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class ServletTMP1 extends HttpServlet implements IDatabaseConnect  {
	
	private static final long serialVersionUID = 1L;
       

    public ServletTMP1() {
        super();
      
    }

    public void PostAndGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	//int bufferSize = response.getBufferSize();
    	
    	ServletTMP1 baza1 = new ServletTMP1();
    	
    	String string = "siemano z tej strony DJ JA-JA-JACA";

    	PrintWriter writer_1 = response.getWriter();
    	PrintWriter writer_2 = response.getWriter();
    	
    	writer_1.write("<html><head></head><body><h1>Hello world! " + string + "</h1><br><font color=blue");
    	
    	baza1.readDataBase(request, response);
    
    	writer_2.write("koniec programu</body></head>");
    	
    	
    }
    	

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			
			PostAndGet(request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

//
//	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		try {
//			PostAndGet(request, response);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
//	

	
	private Connection connect = null;
	private Statement statement = null;
	//private PreparedStatement preparedStatement = null;
	private ResultSet resultSet = null;

	
	public void readDataBase(HttpServletRequest request, HttpServletResponse response ) throws Exception {
		try {
		
			Class.forName("com.mysql.jdbc.Driver");
			
			connect = DriverManager.getConnection(IDataBase);

			
			statement = connect.createStatement(); 
			
			resultSet = statement.executeQuery("select * from COMMENTS");
			
			
			writeResultSet(resultSet, request, response);
			
		} catch (Exception e) {
			throw e;
		} finally {
			close();
		}

	}
	
	
	private void writeResultSet(ResultSet resultSet,HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
	
		while (resultSet.next()) {

			String user = resultSet.getString("myuser");
			String website = resultSet.getString("webpage");
			String summery = resultSet.getString("summery");
			Date date = resultSet.getDate("datum");
			String comment = resultSet.getString("comments");
			
		
			PrintWriter writer = response.getWriter();
			writer.write("<br>UserSTRING: " + user);
			writer.write("<br>WebsiteSTRING: " + website);
			writer.write("<br>SummerySTRING: " + summery);
			writer.write("<br>DateSTRING: " + date);
			writer.write("<br>CommentSTRING: " + comment);
			writer.write("<br>- - - - - - - - <br>");
			

		}
	}

	
	private void close() {
		try {
			if (resultSet != null) {
				resultSet.close();
			}
			if (statement != null) {
				statement.close();
			}
			if (connect != null) {
				connect.close();
			}
		} catch (Exception e) {

		}
	
}
}
